package com.sqmf.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class EncryptUtils2 {

    public static final String MD5 = "MD5";

    /**
     * BASE64加密
     *
     * @param data
     *            要加密的字符
     * @return
     */
    public static String encryptBASE64(String data) {
        try {
            return (new BASE64Encoder()).encodeBuffer(data.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return data;
        }
    }

    /**
     * BASE64解密
     *
     * @param data
     *            要解密的字符
     * @return
     */
    public static String decryptBASE64(String data) {
        try {
            return new String((new BASE64Decoder()).decodeBuffer(data), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }

    /**
     * MessageDigest加密
     *
     * @param data
     * @return
     */
    public static String encrypt(String data, String encryptType) {
        return encrypt(data, encryptType, "utf-8");
    }

    /**
     * MessageDigest加密
     *
     * @param data
     * @return
     */
    public static String encrypt(String data, String encryptType,
                                 String encondeType) {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance(encryptType);
            try {
                md.update(data.getBytes(encondeType));
            } catch (UnsupportedEncodingException e) {
                md.update(data.getBytes());
            }
            byte[] b = md.digest();
            StringBuffer buf = new StringBuffer("");
            int i = 0;
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
            return data;
        }
    }

    /**
     * MD5加密
     *
     * @param data
     * @return
     */
    public static String encrypt(String data) {
        return encrypt(data, MD5);
    }

    /**
     * 加密
     *
     * @param input
     *            要加密的字符串
     * @param strkey
     *            密码
     * @return
     * @throws Exception
     */
    public static String desEncrypt(String input, String strkey)
            throws Exception {
        DESedeKeySpec dks = new DESedeKeySpec(encrypt(strkey).getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey securekey = keyFactory.generateSecret(dks);

        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, securekey);
        byte[] b = cipher.doFinal(encryptBASE64(input).getBytes("UTF-8"));
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(b).replaceAll("\r", "").replaceAll("\n", "");
    }

    /**
     * 解密
     *
     * @param cipherText
     *            密串
     * @param strkey
     *            解密密码
     * @return
     * @throws Exception
     */
    public static String desDecrypt(String cipherText, String strkey)
            throws Exception {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bytesrc = decoder.decodeBuffer(cipherText);
        // --解密的key
        DESedeKeySpec dks = new DESedeKeySpec(encrypt(strkey).getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey securekey = keyFactory.generateSecret(dks);

        // --Chipher对象解密
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, securekey);
        byte[] retByte = cipher.doFinal(bytesrc);
        return decryptBASE64(new String(retByte));
    }

    public static String getProKey(String key) {
        Properties properties = new Properties();
        InputStream in = EncryptUtils2.class.getClassLoader()
                .getResourceAsStream("config.properties");
        try {
            properties.load(in);
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String value = properties.getProperty(key);
        return value;
    }

    public static void main(String[] args) {
        try {
            String key = "REYUI2WOPRJK1LDFGNMVLS4AEFOIGO0LLDFVCWEO9IGPQ=";// 加密字串1
            String pro = "13711111111";// 手机号码
            System.out.println("原数据：" + pro);
            String enc = desEncrypt(pro, key);// 加密方法
            System.out.println("加密后的参数：?pro=" + enc);
            String dec = desDecrypt(enc, key);// 解密方法
            System.out.println("解密后的：" + dec);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}